<?php
namespace App\Common;

use Aliyun\Core\Profile\DefaultProfile;
use Aliyun\Core\DefaultAcsClient;
use Aliyun\Core\Exception\ClientException;
use Aliyun\Core\Exception\ServerException;
use Aliyun\Core\Regions\Endpoint;
use Aliyun\Core\Regions\EndpointProvider;
use Aliyun\Core\Regions\EndpointConfig;
use Aliyun\Core\Sms\Request\V20160927\SingleSendSmsRequest;
use Illuminate\Support\Facades\Session;

/*
 * 
 * 使用方法
 * $b=(new AliyunSms()) ->code_type(5421)->setRecNum(16621047203);
 * 
 * 谭金星
 * 2018年4月4日17:59:43
 */

/**
 * 阿里云短信
 */
class AliyunSms{
    static private $setSignName="增多科技";
    static private $access=[
        "regionId"=>"cn-hangzhou",
        "accessKeyId"=>"LTAI8ZG6MxIDJF4b", 
        "accessSecret"=>"XlGnSPRdSMxgW5nuj7EZ4kxd2wBDvd"
    ];
    static private $request;
    static $client='';
    /**
     * 
     * @param type $config 配置参数
     */
    public function __construct($config=[]) {
        if(! empty($config) && is_array($config)){
            self::$access=array_merge(self::$access,$config);
        }
        $number=Session::get("sms.number");
        Session::put("sms.number" ,++$number);
//        if($number > 6){
//            exit(json_encode([
//                        "code"=>10001,
//                        "msg"=> "错误 验证码获取过于频繁",
//                        "data"=>""
//                ]));
//        }
        
        
        $iClientProfile = DefaultProfile::getProfile(self::$access['regionId'], self::$access['accessKeyId'], self::$access['accessSecret']);
        self::$client = new DefaultAcsClient($iClientProfile);
        $config = new EndpointConfig();
        $endpoint = new Endpoint(self::$access['regionId'], $config->getRegionIds(), $config->getProductDomains());
        $endpoints = array( $endpoint );
        EndpointProvider::setEndpoints($endpoints);
        self::$request = new SingleSendSmsRequest();
    }
    /**
     * 设置签名
     * @param string $name 
     * @return $this
     */
    public function setSignName($name){
        self::$setSignName=$name;
        return $this;
    }
    /**
     * $smstype=[
     *      "reg"    ,//注册
     *      "sendpass",//登陆
     *      "notice",//咨询
     *      "newconsult", //质询订单
     *      "globalreg"  //国际注册
     * ]
     * 设置短信模板  和验证码信息
     * @param string $code    验证码code
     * @param string $smstype     短信类别
     */
    public function code_type( $code,$smstype="reg"){
        self::$request->setSignName(self::$setSignName); //签名名称
        switch ($smstype) {
            case 'reg':
                self::$request->setTemplateCode("SMS_33355134");/*模板code*/
                self::$request->setParamString("{\"code\":\"".$code."\",\"product\":\"". self::$setSignName . "\"}");
                break;

            case 'sendpass':
                self::$request->setTemplateCode("SMS_33355136");/*模板code*/
                self::$request->setParamString("{\"code\":\"".$code."\",\"product\":\"". self::$setSignName . "\"}");
                break;

            case 'remind':
                self::$request->setTemplateCode("SMS_33355136");/*模板code*/
                self::$request->setParamString("{\"code\":\"".$code."\",\"product\":\"". self::$setSignName . "\"}");
                break;
                
            case 'notice':
                self::$request->setTemplateCode("SMS_35375032");/*模板code*/
                self::$request->setParamString("{\"code\":\"".$code."\",\"product\":\"". self::$setSignName . "\"}");
                break;

            case 'newconsult':
                self::$request->setTemplateCode("SMS_35830094");/*模板code*/
                self::$request->setParamString("{\"product\":\"". self::$setSignName . "\"}");
                break;

            case 'globalreg':
                self::$request->setTemplateCode("SMS_148861119");/*模板code*/
                self::$request->setParamString("{\"code\":\"".$code."\",\"product\":\"". self::$setSignName . "\"}");
                break;
            default:
                # 默认为注册
                self::$request->setTemplateCode("SMS_33355134");/*模板code*/
                self::$request->setParamString("{\"code\":\"".$code."\",\"product\":\"". self::$setSignName . "\"}");
                break;
        }
        return $this;
    }
    /**
     * 接受者电话号码
     * @param type $setRecNum 电话号码
     */
    public function setRecNum($setRecNum){
        
        if(! preg_match("/^1[34578]\d{9}$/", $setRecNum)){
          return [
                "code"=>10001,
                "msg"=> "错误非手机号码，请正确填写手机号码",
                "data"=>["ErrorCode"=>0 ,"ErrorMessage"=>"不是电话号码" ]
            ];
        }
        self::$request->setRecNum($setRecNum);
        try {
            $response = self::$client->getAcsResponse(self::$request);
            return [
                "code"=>666,
                "msg"=> "短信已发出",
                "data"=>$response
            ];
        }
        catch (ClientException  $e) {
            return [
                        "code"=>10001,
                        "msg"=> "错误",
                        "data"=>$e->getErrorMessage()
                    ];
        }
        catch (ServerException  $e) {        
            return [
                        "code"=>10001,
                        "msg"=> "错误",
                        "data"=>$e->getErrorMessage()
                    ];
        }
    }
}



